package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AssetListBean;
import com.sskj.cndragon.bean.IncomesBean;
import com.sskj.cndragon.bean.MyTeamListBean;
import com.sskj.cndragon.presenter.MySpreadActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.TabEntity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.HideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;


public class MySpreadActivity extends BaseActivity<MySpreadActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.tv_team)
    TextView tvTeam;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter<MyTeamListBean.MyTeamBean> slimAdapter;
    private MVCCoolHelper<List<MyTeamListBean.MyTeamBean>> mvcHelper;


    private String[] mTitles = {App.INSTANCE.getString(R.string.appYidai), App.INSTANCE.getString(R.string.appErdai), App.INSTANCE.getString(R.string.appSandai)};
    private int type = 1;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_spread;
    }

    @Override
    public MySpreadActivityPresenter getPresenter() {
        return new MySpreadActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setRightButtonOnClickListener(view -> SpreadActivity.start(this));


        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                type = position + 1;
                if (mvcHelper != null) {
                    mvcHelper.refresh();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ClickUtil.click(300, view1, () -> {
            SpreadRecordActivity.start(this, App.INSTANCE.getString(R.string.yidaishouyi), "1");
        });
        ClickUtil.click(300, view2, () -> {
            SpreadRecordActivity.start(this, App.INSTANCE.getString(R.string.erdaishouyi), "2");
        });
        ClickUtil.click(300, view3, () -> {
            SpreadRecordActivity.start(this, App.INSTANCE.getString(R.string.sandaishouyi), "3");
        });
        ClickUtil.click(300, view4, () -> {
            SpreadRecordActivity.start(this, App.INSTANCE.getString(R.string.appTuanduishouyi), "4");
        });
        slimAdapter = SlimAdapter.create().register(R.layout.item_spread_zhuce, new SlimInjector<MyTeamListBean.MyTeamBean>() {
            @Override
            public void onInject(MyTeamListBean.MyTeamBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_mobile, CheckUtil.isMobile(data.getTel())?HideUtil.getPhoneHide(data.getTel()):data.getTel())
                        .text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<MyTeamListBean.MyTeamBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<MyTeamListBean.MyTeamBean>)
                        page -> mPresenter.getMyTeamRecord(type, page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getIncomes(SPUtil.get(SPConfig.ID, ""));
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MySpreadActivity.class);
        context.startActivity(intent);
    }

    public void getIncomesOK(IncomesBean incomesBean) {
        if (incomesBean != null) {
            tvOne.setText(NumberUtil.keep2(incomesBean.getFirstEarning()));
            tvTwo.setText(NumberUtil.keep2(incomesBean.getSecondEarning()));
            tvThree.setText(NumberUtil.keep2(incomesBean.getThirdEarning()));
            tvTeam.setText(NumberUtil.keep2(incomesBean.getTeamEarning()));
        }
    }
}
